<?php
class BatchController extends BaseController
{
    // {$url}/batch/order
    public function orderAction()
    {
        $db = $this->mysql();
        $order = $db->select('aggregation_batch_order','*',[
            'LIMIT' => 100,
            'deletetime'=>null,
            'remarks' => ''
        ]);
        foreach ($order as $key => $value) {
            $auth = $db->get('aggregation_authorize','*',[
                'authorize_number' => $value['authorize_number'],
                'deletetime'       => null,
            ]);
            if(is_null($auth)){
                continue;
            }
            $data      = [
                "order_number" => $value['order_number'],
                "mobile"       => $value['mobile'],
                "amount"       => $value['amount'],
                "notify_url"   => $value['notify_url'],
            ];
            $data['sign'] = makeSign($data, $auth['appsecret']);
            $res = send_post('https://recharge.xianledian.com/v1/api/mobilerecharge/recharge?appkey=' . $auth['appkey'], $data);
            $res = json_decode($res,true);
            if($res['code'] == '10000'){
                $db->update('aggregation_batch_order',[
                    'deletetime' => time(),
                    'remarks' => '充值成功',
                ],[
                    'id' => $value['id']
                ]);
            }else{
                $db->update('aggregation_batch_order',[
                    'updatetime' => time(),
                    'remarks' => '操作未成功-'.$res['code'].'-'.$res['message']
                ],[
                    'id' => $value['id']
                ]);
            }
        }
        // $db->action(function ($database) {
        //     // 清理已扣除余额，未成功支付，订单
        //     $order = $database->select('cecharge_order', '*', [
        //         'createtime[<]'     => time() - 300,
        //         'paytime'           => 0,
        //         'status'            => 'wait',
        //         'balance[>=]'        => 0,
        //         'payment_amount[>]' => 0,
        //     ]);

        //     if (!empty($order)) {
        //         foreach ($order as $key => $value) {
        //             if($value['balance'] >0){
        //                 // 获取上一级用户信息
        //                 $account_balance = $database->get('user', 'account_balance', [
        //                     'user_number' => $value['user_number'],
        //                 ]);
        //                 // 加入资金变动明细
        //                 $database->insert('user_money_log', [
        //                     'order_number' => $value['order_number'],
        //                     'user_number'  => $value['user_number'],
        //                     'before'       => $account_balance,
        //                     'money'        => $value['balance'],
        //                     'after'        => $account_balance + $value['balance'],
        //                     'state'        => '订单超时退款',
        //                     'type'         => '退款',
        //                     'other'        => $value['user_number'],
        //                     'createtime'   => time(),
        //                 ]);
        //                 if ($database->id() < 1) {
        //                     dump_log('退款： ' . $value['order_number'] . ' 状态： 退款失败', 'cash_bonus_error.txt');
        //                     return false;
        //                 }
    
        //                 // 更新钱包数据
        //                 $res = $database->update('user', [
        //                     'account_balance[+]' => $value['balance'],
        //                     // 'withdrawals'        => $card_member,
    
        //                 ], ['user_number' => $value['user_number']]);
        //                 if ($res->rowCount() == 0) {
        //                     dump_log('退款： ' . $value['order_number'] . ' 状态： 更新用户金额失败', 'cash_bonus_error.txt');
        //                     return false;
        //                 }
        //             }
        //             $res = $database->update('cecharge_order', [
        //                 'status'   => 'error',
        //                 'pay_list' => '订单超时',
        //                 'deletetime'     => time()
        //             ], [
        //                 'order_number' => $value['order_number'],
        //             ]);
        //             if ($res->rowCount() == 0) {
        //                 dump_log('退款： ' . $value['order_number'] . ' 状态： 更新用户金额失败', 'cash_bonus_error.txt');
        //                 return false;
        //             }
        //             var_dump('订单：' . $value['order_number'] . ' 成功为' . $value['user_number'] . '退款' . $value['balance'] . '元');

        //         }
        //         return true;
        //     }

        // });

        return false;
    }


}
